/******************************************************************************/
package jamie.org.sil.www.usxConverter;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Stack;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;

import jamie.org.sil.www.usxUtils.*;



/******************************************************************************/
/**
 * A class which parses XML input using SAX, and then routes each tag to
 * the appropriate handler.
 * <p>
 *
 * One important note.  The USX which passes through the present class is
 * modified slightly from that which is fed into it.  We decided some time
 * ago that we wanted each scripture book to have a toc1 entry, so that
 * we could build tables of contents.  However, relatively few translators
 * actually provide one.  To address this, the present class can be
 * configured (and by default <i>is</i> configured) to autogenerate such
 * an item immediately after the book tag.
 * <p>
 *
 * The main activity of the class is to recognise the incoming USX and then
 * route each tag to a method which handles it.  Default implementations
 * of methods are provided in UsxConverterTo; individual converters can
 * then override these as necessary.
 * <p>
 *
 * A few things which it might be useful to know in order to avoid
 * confusion ...
 *
 * <ul>
 *   <li>Some converters need to know about the nesting of tags, because
 *       they may need to process tag B differently depending upon
 *       whether it is located under tag A or tag C.  The class maintains
         an internal stack to handle this.  There are various
 *       methods with names like <i>tagStackWithLevelsContains</i>
 *       which let you query the content of the stack readily.  Note
 *       that a new tag is added to the tag stack <i>before</i> any
 *       tag-specific "start" processing for that tag, and is removed
 *       <i>after</i> any tag-specific "end" processing.</li>
 *
 *   <li>Some converters need to know which para tag we have just finished
 *       processing (for example because a subheader following text
 *       needs to have space before it, where a subheader following
 *       another header does not.  {@link getMostParaRecentTagDetails}
 *       provides the relevant information.  The details are updated
 *       after any tag-specific "end" processing.</li>
 *
 *   <li>There are also some stacks holding tag information and
 *       details of user-defined tags which are used purely internally.</li>
 * </ul>
 * 
 * @author ARA "Jamie" Jamieson
**/
   
public class UsxParser implements ContentHandler
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                                 Public                                 **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Constructor.

     @param tagHandler Class instance which implements the tag handlers.
  */

  public UsxParser (UsxConverterTo tagHandler)
  {
    m_TagHandler = tagHandler;

    m_SwitchTable.put("hash_comment", 1);
    m_SwitchTable.put("hash_text", 2);
    m_SwitchTable.put("userDefinedPara_bra_introductoryMaterial", 3);
    m_SwitchTable.put("userDefinedPara_ket_introductoryMaterial", 4);
    m_SwitchTable.put("userDefinedPara_bra_ul", 5);
    m_SwitchTable.put("userDefinedPara_ket_ul", 6);
    m_SwitchTable.put("book_id", 7);
    m_SwitchTable.put("c", 8);
    m_SwitchTable.put("chapter_c", 9);
    m_SwitchTable.put("char_abbreviation", 10);
    m_SwitchTable.put("char_add", 11);
    m_SwitchTable.put("char_addpn", 12);
    m_SwitchTable.put("char_bd", 13);
    m_SwitchTable.put("char_bdit", 14);
    m_SwitchTable.put("char_bk", 15);
    m_SwitchTable.put("char_ca", 16);
    m_SwitchTable.put("char_dc", 17);
    m_SwitchTable.put("char_efm", 18);
    m_SwitchTable.put("char_em", 19);
    m_SwitchTable.put("char_fdc", 20);
    m_SwitchTable.put("char_fk", 21);
    m_SwitchTable.put("char_fl", 22);
    m_SwitchTable.put("char_fm", 23);
    m_SwitchTable.put("char_fp", 24);
    m_SwitchTable.put("char_fq", 25);
    m_SwitchTable.put("char_fqa", 26);
    m_SwitchTable.put("char_fr", 27);
    m_SwitchTable.put("char_ft", 28);
    m_SwitchTable.put("char_fv", 29);
    m_SwitchTable.put("char_ior", 30);
    m_SwitchTable.put("char_iqt", 31);
    m_SwitchTable.put("char_it", 32);
    m_SwitchTable.put("char_k", 33);
    m_SwitchTable.put("char_label", 34);
    m_SwitchTable.put("char_nd", 35);
    m_SwitchTable.put("char_ndx", 36);
    m_SwitchTable.put("char_no", 37);
    m_SwitchTable.put("char_ord", 38);
    m_SwitchTable.put("char_pn", 39);
    m_SwitchTable.put("char_pro", 40);
    m_SwitchTable.put("char_qac", 41);
    m_SwitchTable.put("char_qs", 42);
    m_SwitchTable.put("char_qt", 43);
    m_SwitchTable.put("char_rem", 44);
    m_SwitchTable.put("char_rq", 45);
    m_SwitchTable.put("char_sc", 46);
    m_SwitchTable.put("char_sc", 47);
    m_SwitchTable.put("char_sig", 48);
    m_SwitchTable.put("char_sls", 49);
    m_SwitchTable.put("char_tc1", 50);
    m_SwitchTable.put("char_tc2", 51);
    m_SwitchTable.put("char_tc3", 52);
    m_SwitchTable.put("char_tc4", 53);
    m_SwitchTable.put("char_tcr1", 54);
    m_SwitchTable.put("char_tcr2", 55);
    m_SwitchTable.put("char_tcr3", 56);
    m_SwitchTable.put("char_tcr4", 57);
    m_SwitchTable.put("char_th1", 58);
    m_SwitchTable.put("char_th2", 59);
    m_SwitchTable.put("char_th3", 60);
    m_SwitchTable.put("char_th4", 61);
    m_SwitchTable.put("char_thr1", 62);
    m_SwitchTable.put("char_thr2", 63);
    m_SwitchTable.put("char_thr3", 64);
    m_SwitchTable.put("char_thr4", 65);
    m_SwitchTable.put("char_tl", 66);
    m_SwitchTable.put("char_va", 67);
    m_SwitchTable.put("char_vp", 68);
    m_SwitchTable.put("char_w", 69);
    m_SwitchTable.put("char_wg", 70);
    m_SwitchTable.put("char_wh", 71);
    m_SwitchTable.put("char_wj", 72);
    m_SwitchTable.put("char_xdc", 73);
    m_SwitchTable.put("char_xk", 74);
    m_SwitchTable.put("char_xnt", 75);
    m_SwitchTable.put("char_xo", 76);
    m_SwitchTable.put("char_xot", 77);
    m_SwitchTable.put("char_xq", 78);
    m_SwitchTable.put("char_xt", 79);
    m_SwitchTable.put("fig", 80);
    m_SwitchTable.put("note_f", 81);
    m_SwitchTable.put("note_fe", 82);
    m_SwitchTable.put("note_x", 83);
    m_SwitchTable.put("para_b", 84);
    m_SwitchTable.put("para_breaking_verse_paragraph", 85);
    m_SwitchTable.put("para_cd", 86);
    m_SwitchTable.put("para_cl", 87);
    m_SwitchTable.put("para_cl1", 88);
    m_SwitchTable.put("para_cl2", 89);
    m_SwitchTable.put("para_cls", 90);
    m_SwitchTable.put("para_cp", 91);
    m_SwitchTable.put("para_d", 92);
    m_SwitchTable.put("para_h", 93);
    m_SwitchTable.put("para_h1", 94);
    m_SwitchTable.put("para_h2", 95);
    m_SwitchTable.put("para_h3", 96);
    m_SwitchTable.put("para_ib", 97);
    m_SwitchTable.put("para_ide", 98);
    m_SwitchTable.put("para_ie", 99);
    m_SwitchTable.put("para_iex", 100);
    m_SwitchTable.put("para_ili", 101);
    m_SwitchTable.put("para_ili1", 102);
    m_SwitchTable.put("para_ili2", 103);
    m_SwitchTable.put("para_im", 104);
    m_SwitchTable.put("para_imi", 105);
    m_SwitchTable.put("para_imq", 106);
    m_SwitchTable.put("para_imt", 107);
    m_SwitchTable.put("para_imt1", 108);
    m_SwitchTable.put("para_imt2", 109);
    m_SwitchTable.put("para_imt3", 110);
    m_SwitchTable.put("para_imt4", 111);
    m_SwitchTable.put("para_imte", 112);
    m_SwitchTable.put("para_imte1", 113);
    m_SwitchTable.put("para_imte2", 114);
    m_SwitchTable.put("para_io", 115);
    m_SwitchTable.put("para_io1", 116);
    m_SwitchTable.put("para_io2", 117);
    m_SwitchTable.put("para_io3", 118);
    m_SwitchTable.put("para_io4", 119);
    m_SwitchTable.put("para_iot", 120);
    m_SwitchTable.put("para_ip", 121);
    m_SwitchTable.put("para_ipi", 122);
    m_SwitchTable.put("para_ipq", 123);
    m_SwitchTable.put("para_ipr", 124);
    m_SwitchTable.put("para_iq", 125);
    m_SwitchTable.put("para_iq1", 126);
    m_SwitchTable.put("para_iq2", 127);
    m_SwitchTable.put("para_iq3", 128);
    m_SwitchTable.put("para_is", 129);
    m_SwitchTable.put("para_is1", 130);
    m_SwitchTable.put("para_is2", 131);
    m_SwitchTable.put("para_k1", 132);
    m_SwitchTable.put("para_k2", 133);
    m_SwitchTable.put("para_li", 134);
    m_SwitchTable.put("para_li1", 135);
    m_SwitchTable.put("para_li2", 136);
    m_SwitchTable.put("para_li3", 137);
    m_SwitchTable.put("para_li4", 138);
    m_SwitchTable.put("para_lit", 139);
    m_SwitchTable.put("para_m", 140);
    m_SwitchTable.put("para_mi", 141);
    m_SwitchTable.put("para_mr", 142);
    m_SwitchTable.put("para_ms", 143);
    m_SwitchTable.put("para_ms1", 144);
    m_SwitchTable.put("para_ms2", 145);
    m_SwitchTable.put("para_ms3", 146);
    m_SwitchTable.put("para_mt", 147);
    m_SwitchTable.put("para_mt1", 148);
    m_SwitchTable.put("para_mt2", 149);
    m_SwitchTable.put("para_mt3", 150);
    m_SwitchTable.put("para_mt4", 151);
    m_SwitchTable.put("para_mte", 152);
    m_SwitchTable.put("para_mte1", 153);
    m_SwitchTable.put("para_mte2", 154);
    m_SwitchTable.put("para_nb", 155);
    m_SwitchTable.put("para_no_break_verse_paragraph", 156);
    m_SwitchTable.put("para_p", 157);
    m_SwitchTable.put("para_p_introduction", 158);
    m_SwitchTable.put("para_p_letter_closing", 159);
    m_SwitchTable.put("para_p_letter_opening", 160);
    m_SwitchTable.put("para_p_variant", 161);
    m_SwitchTable.put("para_p1", 162);
    m_SwitchTable.put("para_p2", 163);
    m_SwitchTable.put("para_pb", 164);
    m_SwitchTable.put("para_pc", 165);
    m_SwitchTable.put("para_periph", 166);
    m_SwitchTable.put("para_pi", 167);
    m_SwitchTable.put("para_pi1", 168);
    m_SwitchTable.put("para_pi2", 169);
    m_SwitchTable.put("para_pi3", 170);
    m_SwitchTable.put("para_pm", 171);
    m_SwitchTable.put("para_pmc", 172);
    m_SwitchTable.put("para_pmo", 173);
    m_SwitchTable.put("para_pmr", 174);
    m_SwitchTable.put("para_psa", 175);
    m_SwitchTable.put("para_psa_conclusion", 176);
    m_SwitchTable.put("para_psa_continuation", 177);
    m_SwitchTable.put("para_psa_introduction", 178);
    m_SwitchTable.put("para_q", 179);
    m_SwitchTable.put("para_q1", 180);
    m_SwitchTable.put("para_q2", 181);
    m_SwitchTable.put("para_q3", 182);
    m_SwitchTable.put("para_q4", 183);
    m_SwitchTable.put("para_qa", 184);
    m_SwitchTable.put("para_qc", 185);
    m_SwitchTable.put("para_qm", 186);
    m_SwitchTable.put("para_qm1", 187);
    m_SwitchTable.put("para_qm2", 188);
    m_SwitchTable.put("para_qm3", 189);
    m_SwitchTable.put("para_qr", 190);
    m_SwitchTable.put("para_qr_attribution", 191);
    m_SwitchTable.put("para_qr_refrain", 192);
    m_SwitchTable.put("para_qr_selah", 193);
    m_SwitchTable.put("para_r", 194);
    m_SwitchTable.put("para_rem", 195);
    m_SwitchTable.put("para_restore", 196);
    m_SwitchTable.put("para_s", 197);
    m_SwitchTable.put("para_s_variant", 198);
    m_SwitchTable.put("para_s_variant_tail", 199);
    m_SwitchTable.put("para_s1", 200);
    m_SwitchTable.put("para_s2", 201);
    m_SwitchTable.put("para_s3", 202);
    m_SwitchTable.put("para_s4", 203);
    m_SwitchTable.put("para_sp", 204);
    m_SwitchTable.put("para_sr", 205);
    m_SwitchTable.put("para_toc1", 206);
    m_SwitchTable.put("para_toc2", 207);
    m_SwitchTable.put("para_toc3", 208);
    m_SwitchTable.put("para_tr", 209);
    m_SwitchTable.put("reference", 364);
    m_SwitchTable.put("usx", 365);
    m_SwitchTable.put("usxv", 366);
    m_SwitchTable.put("v", 367);
    m_SwitchTable.put("verse_v", 368);



    m_DestinationTable.put("hash_comment", OutputManager.C_Destination_BitBucket);
    m_DestinationTable.put("hash_text", OutputManager.C_Destination_Current);
    m_DestinationTable.put("userDefinedPara_bra_introductoryMaterial", OutputManager.C_Destination_Current);
    m_DestinationTable.put("userDefinedPara_ket_introductoryMaterial", OutputManager.C_Destination_Current);
    m_DestinationTable.put("userDefinedPara_bra_ul", OutputManager.C_Destination_Current);
    m_DestinationTable.put("userDefinedPara_ket_ul", OutputManager.C_Destination_Current);
    m_DestinationTable.put("book_id", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("c", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("chapter_c", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("char_abbreviation", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_add", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_addpn", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_bd", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_bdit", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_bk", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_ca", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_dc", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_efm", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_em", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fdc", OutputManager.C_Destination_Footnotes);
    m_DestinationTable.put("char_fk", OutputManager.C_Destination_Footnotes);
    m_DestinationTable.put("char_fl", OutputManager.C_Destination_Footnotes);
    m_DestinationTable.put("char_fm", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fp", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fq", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fqa", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_ft", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_fv", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_ior", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("char_iqt", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("char_it", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_k", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_label", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_nd", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_ndx", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_no", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_ord", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_pn", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_pro", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_qac", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_qs", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_qt", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_rem", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_rq", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_sc", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_sc", OutputManager.C_Destination_Footnotes);
    m_DestinationTable.put("char_sig", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_sls", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tc1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tc2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tc3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tc4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tcr1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tcr2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tcr3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tcr4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_th1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_th2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_th3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_th4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_thr1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_thr2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_thr3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_thr4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_tl", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_va", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_vp", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_w", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_wg", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_wh", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_wj", OutputManager.C_Destination_Current);
    m_DestinationTable.put("char_xdc", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xk", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xnt", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xo", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xot", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xq", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("char_xt", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("fig", OutputManager.C_Destination_Current);
    m_DestinationTable.put("note_f", OutputManager.C_Destination_Footnotes);
    m_DestinationTable.put("note_fe", OutputManager.C_Destination_Endnotes);
    m_DestinationTable.put("note_x", OutputManager.C_Destination_CrossReferences);
    m_DestinationTable.put("para_b", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_breaking_verse_paragraph", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_cd", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_cl", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_cl1", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_cl2", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_cls", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_cp", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_d", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_h", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_h1", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_h2", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_h3", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_ib", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ide", OutputManager.C_Destination_BitBucket);
    m_DestinationTable.put("para_ie", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iex", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_ili", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ili1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ili2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_im", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imi", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imq", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imt", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imt1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imt2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imt3", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imt4", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imte", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imte1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_imte2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_io", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_io1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_io2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_io3", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_io4", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iot", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ip", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ipi", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ipq", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_ipr", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iq", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iq1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iq2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_iq3", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_is", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_is1", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_is2", OutputManager.C_Destination_Introduction);
    m_DestinationTable.put("para_k1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_k2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_li", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_li1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_li2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_li3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_li4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_lit", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_m", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_mi", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_mr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_ms", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_ms1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_ms2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_ms3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_mt", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_mt1", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_mt2", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_mt3", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_mt4", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_mte", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_mte1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_mte2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_nb", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_no_break_verse_paragraph", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_p", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_p_introduction", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_p_letter_closing", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_p_letter_opening", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_p_variant", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_p1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_p2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pb", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pc", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_periph", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pi", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pi1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pi2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pi3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pm", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pmc", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pmo", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_pmr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_psa", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_psa_conclusion", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_psa_continuation", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_psa_introduction", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_q", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_q1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_q2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_q3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_q4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qa", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qc", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qm", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qm1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qm2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qm3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_qr_attribution", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_qr_refrain", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_qr_selah", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_r", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_rem", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_restore", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_s", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_s_variant", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_s_variant_tail", OutputManager.C_Destination_TBD);
    m_DestinationTable.put("para_s1", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_s2", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_s3", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_s4", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_sp", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_sr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("para_toc1", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_toc2", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_toc3", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("para_tr", OutputManager.C_Destination_Current);
    m_DestinationTable.put("reference", OutputManager.C_Destination_Current);
    m_DestinationTable.put("usx", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("usxv", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("v", OutputManager.C_Destination_Scripture);
    m_DestinationTable.put("verse_v", OutputManager.C_Destination_Scripture);
  }


  /****************************************************************************/
  /**
     Initialises the class.  You need to call this separately, because
     there will be a memory leak if you do it in the constructor.
  */

  public void initialise ()
  {
    m_Parser.setContentHandler(this);
  }

    
  /****************************************************************************/
  /*
     Get details of the most-recently completed para tag.

     @return Details of the tag we have just finished processing, in the form
             tagName ":" style ":" level.
  */

  public String getMostRecentParaTagDetails ()
  {
    return m_MostRecentParaTagDetails;
  }

  
  /****************************************************************************/
  /**
      Converts tagName and style into a string which can be used as a key.

      @param tagName

      @param style

      @return Key
  */
  
  static public String makeKey (String tagName, String style)
  {
    if (0 == style.length())
      return tagName;
    else
      return tagName + "_" + style;
  }

  
  /****************************************************************************/
  /**
      Converts tagName,style and level into a string which can be used as a key.

      @param tagName

      @param style

      @param level

      @return Key
  */
  
  static public String makeKey (String tagName, String style, int level)
  {
    String s = makeKey(tagName, style);
    return s + (0 == level ? "" : level);
  }

    
  /****************************************************************************/
  /**
     Parses an individual file.

     @param fileName

     @throws
   */

   public void parseFile (String fileName) throws Exception
   {
     m_Parser.parse(fileName);
   }


  /****************************************************************************/
  /**
     Arranges for toc1 to be autogenerated (or not).  See class comments
     for more details.

     @param val Autogenerate if true.
  */

  public void setAutoGenerateToc1 (boolean val)
  {
    m_AutoGenerateToc1 = val;
  }
  
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the with-levels
     tag-stack.

     @param key
     
     @return True if given item appears in the with-levels tag stack.
  */

  public boolean tagStackWithLevelsContains (String key)
  {
    key = "." + key + ".";
    return -1 != m_ActiveTagsWithLevels.indexOf(key);
  }
 
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the with-levels
     tag-stack.

     @param tagName

     @param style

     @return True if given item appears in the with-levels tag stack.
  */

  public boolean tagStackWithLevelsContains (String tagName, String style)
  {
    String key = "." + makeKey(tagName, style) + ".";
    return -1 != m_ActiveTagsWithLevels.indexOf(key);
  }
 
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the with-levels
     tag-stack.

     @param tagName

     @param style

     @param level
     
     @return True if given item appears in the with-levels tag stack.
  */

  public boolean tagStackWithLevelsContains (String tagName, String style, int level)
  {
    String key = "." + makeKey(tagName, style, level) + ".";
    return -1 != m_ActiveTagsWithLevels.indexOf(key);
  }
 
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the without-levels
     tag-stack.

     @param key
     
     @return True if given item appears in the without-levels tag stack.
  */

  public boolean tagStackWithoutLevelsContains (String key)
  {
    key = "." + key + ".";
    return -1 != m_ActiveTagsWithoutLevels.indexOf(key);
  }
 
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the without-levels
     tag-stack.

     @param tagName

     @param style

     @return True if given item appears in the without-levels tag stack.
  */

  public boolean tagStackWithoutLevelsContains (String tagName, String style)
  {
    String key = "." + makeKey(tagName, style) + ".";
    return -1 != m_ActiveTagsWithoutLevels.indexOf(key);
  }
 
  
  /****************************************************************************/
  /**
     Some converters need to take special action when they encounter tags
     in a specific context (ie tag C under tag A may need to be treated
     differently from tag C under tag B).

     This method checks whether a given item appears in the without-levels
     tag-stack.

     @param tagName

     @param style

     @param level
     
     @return True if given item appears in the without-levels tag stack.
  */

  public boolean tagStackWithoutLevelsContains (String tagName, String style, int level)
  {
    String key = "." + makeKey(tagName, style, level) + ".";
    return -1 != m_ActiveTagsWithoutLevels.indexOf(key);
  }


 
  
  
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                                 Private                                **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /* Converts a tagname to canonical form -- ie the form used here for
     lookups. */

  private String canonicaliseTagName (String tagName)
  {
    if (tagName.startsWith("#")) tagName = "hash_" + tagName.substring(1);
    return tagName;
  }

  
  /****************************************************************************/
  /* Parses tag names into the letter portion and the numbered portion (eg
     toc1).  The return value is the two parts, separated by "_".  If there
     is no number, it is returned as zero. */

  private String parseStyle (String style)
  {
    Matcher m = m_NumberedTagPattern.matcher(style);
    m.matches();
    return m.group(1) + ":" + (0 == m.group(2).length() ? "0" : m.group(2));
  }
  
  
  /****************************************************************************/
  /* Pops the top entry off the active tags stacks. */

  private void popActiveTags ()
  {
    m_ActiveTagsWithLevels = m_ActiveTagsWithLevels.substring(0, m_ActiveTagsWithLevels.length() - 1);
    m_ActiveTagsWithLevels = m_ActiveTagsWithLevels.substring(0, m_ActiveTagsWithLevels.lastIndexOf("."));
    m_ActiveTagsWithoutLevels = m_ActiveTagsWithoutLevels.substring(0, m_ActiveTagsWithoutLevels.length() - 1);
    m_ActiveTagsWithoutLevels = m_ActiveTagsWithoutLevels.substring(0, m_ActiveTagsWithoutLevels.lastIndexOf("."));
  }

  
  /****************************************************************************/
  /**
     Routes control to the appropriate end-of-tag routine.

     @param tagName

     @throws Exception
  **/

  private void routeEnd (ElementDetails eltDetails) throws Exception
  {
    /**************************************************************************/
    /* The stack gives us tag, style and level. */

    String[] detailsFromStack = m_TagStack.pop().split(":");
    String tagNameFromStack = detailsFromStack[0];
    String style = detailsFromStack[1];
    int level = Integer.parseInt(detailsFromStack[2]);
    boolean userDefinedRem = false;



    /**************************************************************************/
    /* The parser will merely have provided us with the tag name (char,
       para, etc).  There's a certain very minimal amount of checking we
       can do ... */
    
    String tagName = canonicaliseTagName(eltDetails.getName());
    if (!tagName.equals(tagNameFromStack))
      throw new ConverterException(ConverterException.Action.AbandonFileNow,
                                   ConverterException.Log.Error,
    				   "Tag stack corrupt");



    /**************************************************************************/
    String key = makeKey(tagName, style, level);



    /**************************************************************************/
    int destination = m_DestinationTable.get(key);
    if ("rem".equals(style))
    {
      String s = m_UserDefinedRem.pop();
      if (null != s)
      {
        key = "userDefined" +
	        tagName.substring(0, 1).toUpperCase() + tagName.substring(1) +
	          "_" + s;
        destination = m_DestinationTable.get(key);
      }
    }

    
    /**************************************************************************/
    m_TagHandler.beforeUsxEndTag(tagName, style, level, eltDetails, destination);
    
    switch (m_SwitchTable.get(key))
    {
      case 1: m_TagHandler.end_hash_comment(tagName, style, level, eltDetails, destination); break;
      case 2: m_TagHandler.end_hash_text(tagName, style, level, eltDetails, destination); break;
      case 3: m_TagHandler.end_userDefinedPara_bra_introductoryMaterial(tagName, style, level, eltDetails, destination); break;
      case 4: m_TagHandler.end_userDefinedPara_ket_introductoryMaterial(tagName, style, level, eltDetails, destination); break;
      case 5: m_TagHandler.end_userDefinedPara_bra_ul(tagName, style, level, eltDetails, destination); break;
      case 6: m_TagHandler.end_userDefinedPara_ket_ul(tagName, style, level, eltDetails, destination); break;
      case 7: m_TagHandler.end_book_id(tagName, style, level, eltDetails, destination); break;
      case 8: m_TagHandler.end_c(tagName, style, level, eltDetails, destination); break;
      case 9: m_TagHandler.end_chapter_c(tagName, style, level, eltDetails, destination); break;
      case 10: m_TagHandler.end_char_abbreviation(tagName, style, level, eltDetails, destination); break;
      case 11: m_TagHandler.end_char_add(tagName, style, level, eltDetails, destination); break;
      case 12: m_TagHandler.end_char_addpn(tagName, style, level, eltDetails, destination); break;
      case 13: m_TagHandler.end_char_bd(tagName, style, level, eltDetails, destination); break;
      case 14: m_TagHandler.end_char_bdit(tagName, style, level, eltDetails, destination); break;
      case 15: m_TagHandler.end_char_bk(tagName, style, level, eltDetails, destination); break;
      case 16: m_TagHandler.end_char_ca(tagName, style, level, eltDetails, destination); break;
      case 17: m_TagHandler.end_char_dc(tagName, style, level, eltDetails, destination); break;
      case 18: m_TagHandler.end_char_efm(tagName, style, level, eltDetails, destination); break;
      case 19: m_TagHandler.end_char_em(tagName, style, level, eltDetails, destination); break;
      case 20: m_TagHandler.end_char_fdc(tagName, style, level, eltDetails, destination); break;
      case 21: m_TagHandler.end_char_fk(tagName, style, level, eltDetails, destination); break;
      case 22: m_TagHandler.end_char_fl(tagName, style, level, eltDetails, destination); break;
      case 23: m_TagHandler.end_char_fm(tagName, style, level, eltDetails, destination); break;
      case 24: m_TagHandler.end_char_fp(tagName, style, level, eltDetails, destination); break;
      case 25: m_TagHandler.end_char_fq(tagName, style, level, eltDetails, destination); break;
      case 26: m_TagHandler.end_char_fqa(tagName, style, level, eltDetails, destination); break;
      case 27: m_TagHandler.end_char_fr(tagName, style, level, eltDetails, destination); break;
      case 28: m_TagHandler.end_char_ft(tagName, style, level, eltDetails, destination); break;
      case 29: m_TagHandler.end_char_fv(tagName, style, level, eltDetails, destination); break;
      case 30: m_TagHandler.end_char_ior(tagName, style, level, eltDetails, destination); break;
      case 31: m_TagHandler.end_char_iqt(tagName, style, level, eltDetails, destination); break;
      case 32: m_TagHandler.end_char_it(tagName, style, level, eltDetails, destination); break;
      case 33: m_TagHandler.end_char_k(tagName, style, level, eltDetails, destination); break;
      case 34: m_TagHandler.end_char_label(tagName, style, level, eltDetails, destination); break;
      case 35: m_TagHandler.end_char_nd(tagName, style, level, eltDetails, destination); break;
      case 36: m_TagHandler.end_char_ndx(tagName, style, level, eltDetails, destination); break;
      case 37: m_TagHandler.end_char_no(tagName, style, level, eltDetails, destination); break;
      case 38: m_TagHandler.end_char_ord(tagName, style, level, eltDetails, destination); break;
      case 39: m_TagHandler.end_char_pn(tagName, style, level, eltDetails, destination); break;
      case 40: m_TagHandler.end_char_pro(tagName, style, level, eltDetails, destination); break;
      case 41: m_TagHandler.end_char_qac(tagName, style, level, eltDetails, destination); break;
      case 42: m_TagHandler.end_char_qs(tagName, style, level, eltDetails, destination); break;
      case 43: m_TagHandler.end_char_qt(tagName, style, level, eltDetails, destination); break;
      case 44: m_TagHandler.end_char_rem(tagName, style, level, eltDetails, destination); break;
      case 45: m_TagHandler.end_char_rq(tagName, style, level, eltDetails, destination); break;
      case 46: m_TagHandler.end_char_sc(tagName, style, level, eltDetails, destination); break;
      case 47: m_TagHandler.end_char_sc(tagName, style, level, eltDetails, destination); break;
      case 48: m_TagHandler.end_char_sig(tagName, style, level, eltDetails, destination); break;
      case 49: m_TagHandler.end_char_sls(tagName, style, level, eltDetails, destination); break;
      case 50: m_TagHandler.end_char_tc1(tagName, style, level, eltDetails, destination); break;
      case 51: m_TagHandler.end_char_tc2(tagName, style, level, eltDetails, destination); break;
      case 52: m_TagHandler.end_char_tc3(tagName, style, level, eltDetails, destination); break;
      case 53: m_TagHandler.end_char_tc4(tagName, style, level, eltDetails, destination); break;
      case 54: m_TagHandler.end_char_tcr1(tagName, style, level, eltDetails, destination); break;
      case 55: m_TagHandler.end_char_tcr2(tagName, style, level, eltDetails, destination); break;
      case 56: m_TagHandler.end_char_tcr3(tagName, style, level, eltDetails, destination); break;
      case 57: m_TagHandler.end_char_tcr4(tagName, style, level, eltDetails, destination); break;
      case 58: m_TagHandler.end_char_th1(tagName, style, level, eltDetails, destination); break;
      case 59: m_TagHandler.end_char_th2(tagName, style, level, eltDetails, destination); break;
      case 60: m_TagHandler.end_char_th3(tagName, style, level, eltDetails, destination); break;
      case 61: m_TagHandler.end_char_th4(tagName, style, level, eltDetails, destination); break;
      case 62: m_TagHandler.end_char_thr1(tagName, style, level, eltDetails, destination); break;
      case 63: m_TagHandler.end_char_thr2(tagName, style, level, eltDetails, destination); break;
      case 64: m_TagHandler.end_char_thr3(tagName, style, level, eltDetails, destination); break;
      case 65: m_TagHandler.end_char_thr4(tagName, style, level, eltDetails, destination); break;
      case 66: m_TagHandler.end_char_tl(tagName, style, level, eltDetails, destination); break;
      case 67: m_TagHandler.end_char_va(tagName, style, level, eltDetails, destination); break;
      case 68: m_TagHandler.end_char_vp(tagName, style, level, eltDetails, destination); break;
      case 69: m_TagHandler.end_char_w(tagName, style, level, eltDetails, destination); break;
      case 70: m_TagHandler.end_char_wg(tagName, style, level, eltDetails, destination); break;
      case 71: m_TagHandler.end_char_wh(tagName, style, level, eltDetails, destination); break;
      case 72: m_TagHandler.end_char_wj(tagName, style, level, eltDetails, destination); break;
      case 73: m_TagHandler.end_char_xdc(tagName, style, level, eltDetails, destination); break;
      case 74: m_TagHandler.end_char_xk(tagName, style, level, eltDetails, destination); break;
      case 75: m_TagHandler.end_char_xnt(tagName, style, level, eltDetails, destination); break;
      case 76: m_TagHandler.end_char_xo(tagName, style, level, eltDetails, destination); break;
      case 77: m_TagHandler.end_char_xot(tagName, style, level, eltDetails, destination); break;
      case 78: m_TagHandler.end_char_xq(tagName, style, level, eltDetails, destination); break;
      case 79: m_TagHandler.end_char_xt(tagName, style, level, eltDetails, destination); break;
      case 80: m_TagHandler.end_fig(tagName, style, level, eltDetails, destination); break;
      case 81: m_TagHandler.end_note_f(tagName, style, level, eltDetails, destination); break;
      case 82: m_TagHandler.end_note_fe(tagName, style, level, eltDetails, destination); break;
      case 83: m_TagHandler.end_note_x(tagName, style, level, eltDetails, destination); break;
      case 84: m_TagHandler.end_para_b(tagName, style, level, eltDetails, destination); break;
      case 85: m_TagHandler.end_para_breaking_verse_paragraph(tagName, style, level, eltDetails, destination); break;
      case 86: m_TagHandler.end_para_cd(tagName, style, level, eltDetails, destination); break;
      case 87: m_TagHandler.end_para_cl(tagName, style, level, eltDetails, destination); break;
      case 88: m_TagHandler.end_para_cl1(tagName, style, level, eltDetails, destination); break;
      case 89: m_TagHandler.end_para_cl2(tagName, style, level, eltDetails, destination); break;
      case 90: m_TagHandler.end_para_cls(tagName, style, level, eltDetails, destination); break;
      case 91: m_TagHandler.end_para_cp(tagName, style, level, eltDetails, destination); break;
      case 92: m_TagHandler.end_para_d(tagName, style, level, eltDetails, destination); break;
      case 93: m_TagHandler.end_para_h(tagName, style, level, eltDetails, destination); break;
      case 94: m_TagHandler.end_para_h1(tagName, style, level, eltDetails, destination); break;
      case 95: m_TagHandler.end_para_h2(tagName, style, level, eltDetails, destination); break;
      case 96: m_TagHandler.end_para_h3(tagName, style, level, eltDetails, destination); break;
      case 97: m_TagHandler.end_para_ib(tagName, style, level, eltDetails, destination); break;
      case 98: m_TagHandler.end_para_ide(tagName, style, level, eltDetails, destination); break;
      case 99: m_TagHandler.end_para_ie(tagName, style, level, eltDetails, destination); break;
      case 100: m_TagHandler.end_para_iex(tagName, style, level, eltDetails, destination); break;
      case 101: m_TagHandler.end_para_ili(tagName, style, level, eltDetails, destination); break;
      case 102: m_TagHandler.end_para_ili1(tagName, style, level, eltDetails, destination); break;
      case 103: m_TagHandler.end_para_ili2(tagName, style, level, eltDetails, destination); break;
      case 104: m_TagHandler.end_para_im(tagName, style, level, eltDetails, destination); break;
      case 105: m_TagHandler.end_para_imi(tagName, style, level, eltDetails, destination); break;
      case 106: m_TagHandler.end_para_imq(tagName, style, level, eltDetails, destination); break;
      case 107: m_TagHandler.end_para_imt(tagName, style, level, eltDetails, destination); break;
      case 108: m_TagHandler.end_para_imt1(tagName, style, level, eltDetails, destination); break;
      case 109: m_TagHandler.end_para_imt2(tagName, style, level, eltDetails, destination); break;
      case 110: m_TagHandler.end_para_imt3(tagName, style, level, eltDetails, destination); break;
      case 111: m_TagHandler.end_para_imt4(tagName, style, level, eltDetails, destination); break;
      case 112: m_TagHandler.end_para_imte(tagName, style, level, eltDetails, destination); break;
      case 113: m_TagHandler.end_para_imte1(tagName, style, level, eltDetails, destination); break;
      case 114: m_TagHandler.end_para_imte2(tagName, style, level, eltDetails, destination); break;
      case 115: m_TagHandler.end_para_io(tagName, style, level, eltDetails, destination); break;
      case 116: m_TagHandler.end_para_io1(tagName, style, level, eltDetails, destination); break;
      case 117: m_TagHandler.end_para_io2(tagName, style, level, eltDetails, destination); break;
      case 118: m_TagHandler.end_para_io3(tagName, style, level, eltDetails, destination); break;
      case 119: m_TagHandler.end_para_io4(tagName, style, level, eltDetails, destination); break;
      case 120: m_TagHandler.end_para_iot(tagName, style, level, eltDetails, destination); break;
      case 121: m_TagHandler.end_para_ip(tagName, style, level, eltDetails, destination); break;
      case 122: m_TagHandler.end_para_ipi(tagName, style, level, eltDetails, destination); break;
      case 123: m_TagHandler.end_para_ipq(tagName, style, level, eltDetails, destination); break;
      case 124: m_TagHandler.end_para_ipr(tagName, style, level, eltDetails, destination); break;
      case 125: m_TagHandler.end_para_iq(tagName, style, level, eltDetails, destination); break;
      case 126: m_TagHandler.end_para_iq1(tagName, style, level, eltDetails, destination); break;
      case 127: m_TagHandler.end_para_iq2(tagName, style, level, eltDetails, destination); break;
      case 128: m_TagHandler.end_para_iq3(tagName, style, level, eltDetails, destination); break;
      case 129: m_TagHandler.end_para_is(tagName, style, level, eltDetails, destination); break;
      case 130: m_TagHandler.end_para_is1(tagName, style, level, eltDetails, destination); break;
      case 131: m_TagHandler.end_para_is2(tagName, style, level, eltDetails, destination); break;
      case 132: m_TagHandler.end_para_k1(tagName, style, level, eltDetails, destination); break;
      case 133: m_TagHandler.end_para_k2(tagName, style, level, eltDetails, destination); break;
      case 134: m_TagHandler.end_para_li(tagName, style, level, eltDetails, destination); break;
      case 135: m_TagHandler.end_para_li1(tagName, style, level, eltDetails, destination); break;
      case 136: m_TagHandler.end_para_li2(tagName, style, level, eltDetails, destination); break;
      case 137: m_TagHandler.end_para_li3(tagName, style, level, eltDetails, destination); break;
      case 138: m_TagHandler.end_para_li4(tagName, style, level, eltDetails, destination); break;
      case 139: m_TagHandler.end_para_lit(tagName, style, level, eltDetails, destination); break;
      case 140: m_TagHandler.end_para_m(tagName, style, level, eltDetails, destination); break;
      case 141: m_TagHandler.end_para_mi(tagName, style, level, eltDetails, destination); break;
      case 142: m_TagHandler.end_para_mr(tagName, style, level, eltDetails, destination); break;
      case 143: m_TagHandler.end_para_ms(tagName, style, level, eltDetails, destination); break;
      case 144: m_TagHandler.end_para_ms1(tagName, style, level, eltDetails, destination); break;
      case 145: m_TagHandler.end_para_ms2(tagName, style, level, eltDetails, destination); break;
      case 146: m_TagHandler.end_para_ms3(tagName, style, level, eltDetails, destination); break;
      case 147: m_TagHandler.end_para_mt(tagName, style, level, eltDetails, destination); break;
      case 148: m_TagHandler.end_para_mt1(tagName, style, level, eltDetails, destination); break;
      case 149: m_TagHandler.end_para_mt2(tagName, style, level, eltDetails, destination); break;
      case 150: m_TagHandler.end_para_mt3(tagName, style, level, eltDetails, destination); break;
      case 151: m_TagHandler.end_para_mt4(tagName, style, level, eltDetails, destination); break;
      case 152: m_TagHandler.end_para_mte(tagName, style, level, eltDetails, destination); break;
      case 153: m_TagHandler.end_para_mte1(tagName, style, level, eltDetails, destination); break;
      case 154: m_TagHandler.end_para_mte2(tagName, style, level, eltDetails, destination); break;
      case 155: m_TagHandler.end_para_nb(tagName, style, level, eltDetails, destination); break;
      case 156: m_TagHandler.end_para_no_break_verse_paragraph(tagName, style, level, eltDetails, destination); break;
      case 157: m_TagHandler.end_para_p(tagName, style, level, eltDetails, destination); break;
      case 158: m_TagHandler.end_para_p_introduction(tagName, style, level, eltDetails, destination); break;
      case 159: m_TagHandler.end_para_p_letter_closing(tagName, style, level, eltDetails, destination); break;
      case 160: m_TagHandler.end_para_p_letter_opening(tagName, style, level, eltDetails, destination); break;
      case 161: m_TagHandler.end_para_p_variant(tagName, style, level, eltDetails, destination); break;
      case 162: m_TagHandler.end_para_p1(tagName, style, level, eltDetails, destination); break;
      case 163: m_TagHandler.end_para_p2(tagName, style, level, eltDetails, destination); break;
      case 164: m_TagHandler.end_para_pb(tagName, style, level, eltDetails, destination); break;
      case 165: m_TagHandler.end_para_pc(tagName, style, level, eltDetails, destination); break;
      case 166: m_TagHandler.end_para_periph(tagName, style, level, eltDetails, destination); break;
      case 167: m_TagHandler.end_para_pi(tagName, style, level, eltDetails, destination); break;
      case 168: m_TagHandler.end_para_pi1(tagName, style, level, eltDetails, destination); break;
      case 169: m_TagHandler.end_para_pi2(tagName, style, level, eltDetails, destination); break;
      case 170: m_TagHandler.end_para_pi3(tagName, style, level, eltDetails, destination); break;
      case 171: m_TagHandler.end_para_pm(tagName, style, level, eltDetails, destination); break;
      case 172: m_TagHandler.end_para_pmc(tagName, style, level, eltDetails, destination); break;
      case 173: m_TagHandler.end_para_pmo(tagName, style, level, eltDetails, destination); break;
      case 174: m_TagHandler.end_para_pmr(tagName, style, level, eltDetails, destination); break;
      case 175: m_TagHandler.end_para_psa(tagName, style, level, eltDetails, destination); break;
      case 176: m_TagHandler.end_para_psa_conclusion(tagName, style, level, eltDetails, destination); break;
      case 177: m_TagHandler.end_para_psa_continuation(tagName, style, level, eltDetails, destination); break;
      case 178: m_TagHandler.end_para_psa_introduction(tagName, style, level, eltDetails, destination); break;
      case 179: m_TagHandler.end_para_q(tagName, style, level, eltDetails, destination); break;
      case 180: m_TagHandler.end_para_q1(tagName, style, level, eltDetails, destination); break;
      case 181: m_TagHandler.end_para_q2(tagName, style, level, eltDetails, destination); break;
      case 182: m_TagHandler.end_para_q3(tagName, style, level, eltDetails, destination); break;
      case 183: m_TagHandler.end_para_q4(tagName, style, level, eltDetails, destination); break;
      case 184: m_TagHandler.end_para_qa(tagName, style, level, eltDetails, destination); break;
      case 185: m_TagHandler.end_para_qc(tagName, style, level, eltDetails, destination); break;
      case 186: m_TagHandler.end_para_qm(tagName, style, level, eltDetails, destination); break;
      case 187: m_TagHandler.end_para_qm1(tagName, style, level, eltDetails, destination); break;
      case 188: m_TagHandler.end_para_qm2(tagName, style, level, eltDetails, destination); break;
      case 189: m_TagHandler.end_para_qm3(tagName, style, level, eltDetails, destination); break;
      case 190: m_TagHandler.end_para_qr(tagName, style, level, eltDetails, destination); break;
      case 191: m_TagHandler.end_para_qr_attribution(tagName, style, level, eltDetails, destination); break;
      case 192: m_TagHandler.end_para_qr_refrain(tagName, style, level, eltDetails, destination); break;
      case 193: m_TagHandler.end_para_qr_selah(tagName, style, level, eltDetails, destination); break;
      case 194: m_TagHandler.end_para_r(tagName, style, level, eltDetails, destination); break;
      case 195: m_TagHandler.end_para_rem(tagName, style, level, eltDetails, destination); break;
      case 196: m_TagHandler.end_para_restore(tagName, style, level, eltDetails, destination); break;
      case 197: m_TagHandler.end_para_s(tagName, style, level, eltDetails, destination); break;
      case 198: m_TagHandler.end_para_s_variant(tagName, style, level, eltDetails, destination); break;
      case 199: m_TagHandler.end_para_s_variant_tail(tagName, style, level, eltDetails, destination); break;
      case 200: m_TagHandler.end_para_s1(tagName, style, level, eltDetails, destination); break;
      case 201: m_TagHandler.end_para_s2(tagName, style, level, eltDetails, destination); break;
      case 202: m_TagHandler.end_para_s3(tagName, style, level, eltDetails, destination); break;
      case 203: m_TagHandler.end_para_s4(tagName, style, level, eltDetails, destination); break;
      case 204: m_TagHandler.end_para_sp(tagName, style, level, eltDetails, destination); break;
      case 205: m_TagHandler.end_para_sr(tagName, style, level, eltDetails, destination); break;
      case 206: m_TagHandler.end_para_toc1(tagName, style, level, eltDetails, destination); break;
      case 207: m_TagHandler.end_para_toc2(tagName, style, level, eltDetails, destination); break;
      case 208: m_TagHandler.end_para_toc3(tagName, style, level, eltDetails, destination); break;
      case 209: m_TagHandler.end_para_tr(tagName, style, level, eltDetails, destination); break;
      case 364: m_TagHandler.end_reference(tagName, style, level, eltDetails, destination); break;
      case 365: m_TagHandler.end_usx(tagName, style, level, eltDetails, destination); break;
      case 366: m_TagHandler.end_usxv(tagName, style, level, eltDetails, destination); break;
      case 367: m_TagHandler.end_v(tagName, style, level, eltDetails, destination); break;
      case 368: m_TagHandler.end_verse_v(tagName, style, level, eltDetails, destination); break;
    }

    m_TagHandler.afterUsxEndTag(tagName, style, level, eltDetails, destination);

    popActiveTags();

    if ("para".equals(tagName) && !"rem".equals(style))
      m_MostRecentParaTagDetails = tagName + ":" + style + ":" + level;
  }


  /****************************************************************************/
  /**
     Routes control to the appropriate start-of-tag routine.

     @param eltDetails Element details.

     @throws Exception
  **/

  private void routeStart (ElementDetails eltDetails) throws Exception
  {
    /**************************************************************************/
    int level = 0;
    String tagName = eltDetails.getName();
    String style = null;
    try { style = eltDetails.getAttribute("style"); } catch (Exception e) {}
    if (null == style) style = "";
    
    tagName = canonicaliseTagName(tagName);
    
    LogManager.instance().debug(0xFF,
                                tagName +
                                (0 == style.length() ? "" : " style=" + style) + 
				((null != eltDetails.getNodeValue() && 0 != eltDetails.getNodeValue().length()) ?
                                " " + eltDetails.getNodeValue() : ""));



    /**************************************************************************/
    /* The key we need to use for lookup purposes is based on the tagname
       and the style. */
    
    String key = makeKey(tagName, style); // The key used in the switch table.
    int destination = m_DestinationTable.get(key);



    /**************************************************************************/
    /* User-defined "rem" needs special processing. */
    
    if ("rem".equals(style))
    {
      String s = eltDetails.getAttribute("userDefined");
      if (null != s)
      {
        key = "userDefined" +
	        tagName.substring(0, 1).toUpperCase() + tagName.substring(1) +
	          "_" + s;
        destination = m_DestinationTable.get(key);
      }

      m_UserDefinedRem.push(s);
    }


    
    /**************************************************************************/
    /* We need to stack not only the tag name, but also the style, because
       we won't have the style available to us when we hit the end tag.
       It's also convenient to parse out the level. */
    
    if (0 != style.length())
    {
      style = parseStyle(style);
      level = Integer.parseInt(style.split(":")[1]);
      style = style.split(":")[0];
    }

    m_TagStack.push(tagName + ":" + style + ":" + level);
    


    /**************************************************************************/
    /* Compare the destinations for the present and previous tag.  If one
       of them is going to the "introduction" destination and the other is
       not, emulate a start or end introduction block here (these are our
       own extensions to USX which let us do things before or after the
       introductory material as a whole.  Note that I rely here on all
       introductory material coming in a single block, with other stuff
       after it. */
    
    if (destination == OutputManager.C_Destination_Introduction &&
        m_PrevDestination != OutputManager.C_Destination_Introduction)
    {
      m_ActiveTagsWithLevels += ".introductionBlock.";
      m_ActiveTagsWithoutLevels += ".introductionBlock.";
      m_TagHandler.beforeUsxStartTag("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
      m_TagHandler.start_extension_introductionBlock("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
      m_TagHandler.afterUsxStartTag("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
    }
    else if (destination != OutputManager.C_Destination_Introduction &&
             destination != OutputManager.C_Destination_Current &&
             m_PrevDestination == OutputManager.C_Destination_Introduction)
    {
      m_TagHandler.beforeUsxEndTag("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
      m_TagHandler.end_extension_introductionBlock("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
      m_TagHandler.afterUsxEndTag("extension", "introduction", 0, null, OutputManager.C_Destination_Introduction);
      popActiveTags();
    }

    if (destination != OutputManager.C_Destination_Current) m_PrevDestination = destination;

    
    
    /**************************************************************************/
    m_ActiveTagsWithLevels += "." + makeKey(tagName, style, level) + ".";
    m_ActiveTagsWithoutLevels += "." + makeKey(tagName, style) + ".";



    /**************************************************************************/
    m_TagHandler.beforeUsxStartTag(tagName, style, level, eltDetails, destination);
    
    switch (m_SwitchTable.get(key))
    {
      case 1: m_TagHandler.start_hash_comment(tagName, style, level, eltDetails, destination); break;
      case 2: m_TagHandler.start_hash_text(tagName, style, level, eltDetails, destination); break;
      case 3: m_TagHandler.start_userDefinedPara_bra_introductoryMaterial(tagName, style, level, eltDetails, destination); break;
      case 4: m_TagHandler.start_userDefinedPara_ket_introductoryMaterial(tagName, style, level, eltDetails, destination); break;
      case 5: m_TagHandler.start_userDefinedPara_bra_ul(tagName, style, level, eltDetails, destination); break;
      case 6: m_TagHandler.start_userDefinedPara_ket_ul(tagName, style, level, eltDetails, destination); break;
      case 7: m_TagHandler.start_book_id(tagName, style, level, eltDetails, destination); break;
      case 8: m_TagHandler.start_c(tagName, style, level, eltDetails, destination); break;
      case 9: m_TagHandler.start_chapter_c(tagName, style, level, eltDetails, destination); break;
      case 10: m_TagHandler.start_char_abbreviation(tagName, style, level, eltDetails, destination); break;
      case 11: m_TagHandler.start_char_add(tagName, style, level, eltDetails, destination); break;
      case 12: m_TagHandler.start_char_addpn(tagName, style, level, eltDetails, destination); break;
      case 13: m_TagHandler.start_char_bd(tagName, style, level, eltDetails, destination); break;
      case 14: m_TagHandler.start_char_bdit(tagName, style, level, eltDetails, destination); break;
      case 15: m_TagHandler.start_char_bk(tagName, style, level, eltDetails, destination); break;
      case 16: m_TagHandler.start_char_ca(tagName, style, level, eltDetails, destination); break;
      case 17: m_TagHandler.start_char_dc(tagName, style, level, eltDetails, destination); break;
      case 18: m_TagHandler.start_char_efm(tagName, style, level, eltDetails, destination); break;
      case 19: m_TagHandler.start_char_em(tagName, style, level, eltDetails, destination); break;
      case 20: m_TagHandler.start_char_fdc(tagName, style, level, eltDetails, destination); break;
      case 21: m_TagHandler.start_char_fk(tagName, style, level, eltDetails, destination); break;
      case 22: m_TagHandler.start_char_fl(tagName, style, level, eltDetails, destination); break;
      case 23: m_TagHandler.start_char_fm(tagName, style, level, eltDetails, destination); break;
      case 24: m_TagHandler.start_char_fp(tagName, style, level, eltDetails, destination); break;
      case 25: m_TagHandler.start_char_fq(tagName, style, level, eltDetails, destination); break;
      case 26: m_TagHandler.start_char_fqa(tagName, style, level, eltDetails, destination); break;
      case 27: m_TagHandler.start_char_fr(tagName, style, level, eltDetails, destination); break;
      case 28: m_TagHandler.start_char_ft(tagName, style, level, eltDetails, destination); break;
      case 29: m_TagHandler.start_char_fv(tagName, style, level, eltDetails, destination); break;
      case 30: m_TagHandler.start_char_ior(tagName, style, level, eltDetails, destination); break;
      case 31: m_TagHandler.start_char_iqt(tagName, style, level, eltDetails, destination); break;
      case 32: m_TagHandler.start_char_it(tagName, style, level, eltDetails, destination); break;
      case 33: m_TagHandler.start_char_k(tagName, style, level, eltDetails, destination); break;
      case 34: m_TagHandler.start_char_label(tagName, style, level, eltDetails, destination); break;
      case 35: m_TagHandler.start_char_nd(tagName, style, level, eltDetails, destination); break;
      case 36: m_TagHandler.start_char_ndx(tagName, style, level, eltDetails, destination); break;
      case 37: m_TagHandler.start_char_no(tagName, style, level, eltDetails, destination); break;
      case 38: m_TagHandler.start_char_ord(tagName, style, level, eltDetails, destination); break;
      case 39: m_TagHandler.start_char_pn(tagName, style, level, eltDetails, destination); break;
      case 40: m_TagHandler.start_char_pro(tagName, style, level, eltDetails, destination); break;
      case 41: m_TagHandler.start_char_qac(tagName, style, level, eltDetails, destination); break;
      case 42: m_TagHandler.start_char_qs(tagName, style, level, eltDetails, destination); break;
      case 43: m_TagHandler.start_char_qt(tagName, style, level, eltDetails, destination); break;
      case 44: m_TagHandler.start_char_rem(tagName, style, level, eltDetails, destination); break;
      case 45: m_TagHandler.start_char_rq(tagName, style, level, eltDetails, destination); break;
      case 46: m_TagHandler.start_char_sc(tagName, style, level, eltDetails, destination); break;
      case 47: m_TagHandler.start_char_sc(tagName, style, level, eltDetails, destination); break;
      case 48: m_TagHandler.start_char_sig(tagName, style, level, eltDetails, destination); break;
      case 49: m_TagHandler.start_char_sls(tagName, style, level, eltDetails, destination); break;
      case 50: m_TagHandler.start_char_tc1(tagName, style, level, eltDetails, destination); break;
      case 51: m_TagHandler.start_char_tc2(tagName, style, level, eltDetails, destination); break;
      case 52: m_TagHandler.start_char_tc3(tagName, style, level, eltDetails, destination); break;
      case 53: m_TagHandler.start_char_tc4(tagName, style, level, eltDetails, destination); break;
      case 54: m_TagHandler.start_char_tcr1(tagName, style, level, eltDetails, destination); break;
      case 55: m_TagHandler.start_char_tcr2(tagName, style, level, eltDetails, destination); break;
      case 56: m_TagHandler.start_char_tcr3(tagName, style, level, eltDetails, destination); break;
      case 57: m_TagHandler.start_char_tcr4(tagName, style, level, eltDetails, destination); break;
      case 58: m_TagHandler.start_char_th1(tagName, style, level, eltDetails, destination); break;
      case 59: m_TagHandler.start_char_th2(tagName, style, level, eltDetails, destination); break;
      case 60: m_TagHandler.start_char_th3(tagName, style, level, eltDetails, destination); break;
      case 61: m_TagHandler.start_char_th4(tagName, style, level, eltDetails, destination); break;
      case 62: m_TagHandler.start_char_thr1(tagName, style, level, eltDetails, destination); break;
      case 63: m_TagHandler.start_char_thr2(tagName, style, level, eltDetails, destination); break;
      case 64: m_TagHandler.start_char_thr3(tagName, style, level, eltDetails, destination); break;
      case 65: m_TagHandler.start_char_thr4(tagName, style, level, eltDetails, destination); break;
      case 66: m_TagHandler.start_char_tl(tagName, style, level, eltDetails, destination); break;
      case 67: m_TagHandler.start_char_va(tagName, style, level, eltDetails, destination); break;
      case 68: m_TagHandler.start_char_vp(tagName, style, level, eltDetails, destination); break;
      case 69: m_TagHandler.start_char_w(tagName, style, level, eltDetails, destination); break;
      case 70: m_TagHandler.start_char_wg(tagName, style, level, eltDetails, destination); break;
      case 71: m_TagHandler.start_char_wh(tagName, style, level, eltDetails, destination); break;
      case 72: m_TagHandler.start_char_wj(tagName, style, level, eltDetails, destination); break;
      case 73: m_TagHandler.start_char_xdc(tagName, style, level, eltDetails, destination); break;
      case 74: m_TagHandler.start_char_xk(tagName, style, level, eltDetails, destination); break;
      case 75: m_TagHandler.start_char_xnt(tagName, style, level, eltDetails, destination); break;
      case 76: m_TagHandler.start_char_xo(tagName, style, level, eltDetails, destination); break;
      case 77: m_TagHandler.start_char_xot(tagName, style, level, eltDetails, destination); break;
      case 78: m_TagHandler.start_char_xq(tagName, style, level, eltDetails, destination); break;
      case 79: m_TagHandler.start_char_xt(tagName, style, level, eltDetails, destination); break;
      case 80: m_TagHandler.start_fig(tagName, style, level, eltDetails, destination); break;
      case 81: m_TagHandler.start_note_f(tagName, style, level, eltDetails, destination); break;
      case 82: m_TagHandler.start_note_fe(tagName, style, level, eltDetails, destination); break;
      case 83: m_TagHandler.start_note_x(tagName, style, level, eltDetails, destination); break;
      case 84: m_TagHandler.start_para_b(tagName, style, level, eltDetails, destination); break;
      case 85: m_TagHandler.start_para_breaking_verse_paragraph(tagName, style, level, eltDetails, destination); break;
      case 86: m_TagHandler.start_para_cd(tagName, style, level, eltDetails, destination); break;
      case 87: m_TagHandler.start_para_cl(tagName, style, level, eltDetails, destination); break;
      case 88: m_TagHandler.start_para_cl1(tagName, style, level, eltDetails, destination); break;
      case 89: m_TagHandler.start_para_cl2(tagName, style, level, eltDetails, destination); break;
      case 90: m_TagHandler.start_para_cls(tagName, style, level, eltDetails, destination); break;
      case 91: m_TagHandler.start_para_cp(tagName, style, level, eltDetails, destination); break;
      case 92: m_TagHandler.start_para_d(tagName, style, level, eltDetails, destination); break;
      case 93: m_TagHandler.start_para_h(tagName, style, level, eltDetails, destination); break;
      case 94: m_TagHandler.start_para_h1(tagName, style, level, eltDetails, destination); break;
      case 95: m_TagHandler.start_para_h2(tagName, style, level, eltDetails, destination); break;
      case 96: m_TagHandler.start_para_h3(tagName, style, level, eltDetails, destination); break;
      case 97: m_TagHandler.start_para_ib(tagName, style, level, eltDetails, destination); break;
      case 98: m_TagHandler.start_para_ide(tagName, style, level, eltDetails, destination); break;
      case 99: m_TagHandler.start_para_ie(tagName, style, level, eltDetails, destination); break;
      case 100: m_TagHandler.start_para_iex(tagName, style, level, eltDetails, destination); break;
      case 101: m_TagHandler.start_para_ili(tagName, style, level, eltDetails, destination); break;
      case 102: m_TagHandler.start_para_ili1(tagName, style, level, eltDetails, destination); break;
      case 103: m_TagHandler.start_para_ili2(tagName, style, level, eltDetails, destination); break;
      case 104: m_TagHandler.start_para_im(tagName, style, level, eltDetails, destination); break;
      case 105: m_TagHandler.start_para_imi(tagName, style, level, eltDetails, destination); break;
      case 106: m_TagHandler.start_para_imq(tagName, style, level, eltDetails, destination); break;
      case 107: m_TagHandler.start_para_imt(tagName, style, level, eltDetails, destination); break;
      case 108: m_TagHandler.start_para_imt1(tagName, style, level, eltDetails, destination); break;
      case 109: m_TagHandler.start_para_imt2(tagName, style, level, eltDetails, destination); break;
      case 110: m_TagHandler.start_para_imt3(tagName, style, level, eltDetails, destination); break;
      case 111: m_TagHandler.start_para_imt4(tagName, style, level, eltDetails, destination); break;
      case 112: m_TagHandler.start_para_imte(tagName, style, level, eltDetails, destination); break;
      case 113: m_TagHandler.start_para_imte1(tagName, style, level, eltDetails, destination); break;
      case 114: m_TagHandler.start_para_imte2(tagName, style, level, eltDetails, destination); break;
      case 115: m_TagHandler.start_para_io(tagName, style, level, eltDetails, destination); break;
      case 116: m_TagHandler.start_para_io1(tagName, style, level, eltDetails, destination); break;
      case 117: m_TagHandler.start_para_io2(tagName, style, level, eltDetails, destination); break;
      case 118: m_TagHandler.start_para_io3(tagName, style, level, eltDetails, destination); break;
      case 119: m_TagHandler.start_para_io4(tagName, style, level, eltDetails, destination); break;
      case 120: m_TagHandler.start_para_iot(tagName, style, level, eltDetails, destination); break;
      case 121: m_TagHandler.start_para_ip(tagName, style, level, eltDetails, destination); break;
      case 122: m_TagHandler.start_para_ipi(tagName, style, level, eltDetails, destination); break;
      case 123: m_TagHandler.start_para_ipq(tagName, style, level, eltDetails, destination); break;
      case 124: m_TagHandler.start_para_ipr(tagName, style, level, eltDetails, destination); break;
      case 125: m_TagHandler.start_para_iq(tagName, style, level, eltDetails, destination); break;
      case 126: m_TagHandler.start_para_iq1(tagName, style, level, eltDetails, destination); break;
      case 127: m_TagHandler.start_para_iq2(tagName, style, level, eltDetails, destination); break;
      case 128: m_TagHandler.start_para_iq3(tagName, style, level, eltDetails, destination); break;
      case 129: m_TagHandler.start_para_is(tagName, style, level, eltDetails, destination); break;
      case 130: m_TagHandler.start_para_is1(tagName, style, level, eltDetails, destination); break;
      case 131: m_TagHandler.start_para_is2(tagName, style, level, eltDetails, destination); break;
      case 132: m_TagHandler.start_para_k1(tagName, style, level, eltDetails, destination); break;
      case 133: m_TagHandler.start_para_k2(tagName, style, level, eltDetails, destination); break;
      case 134: m_TagHandler.start_para_li(tagName, style, level, eltDetails, destination); break;
      case 135: m_TagHandler.start_para_li1(tagName, style, level, eltDetails, destination); break;
      case 136: m_TagHandler.start_para_li2(tagName, style, level, eltDetails, destination); break;
      case 137: m_TagHandler.start_para_li3(tagName, style, level, eltDetails, destination); break;
      case 138: m_TagHandler.start_para_li4(tagName, style, level, eltDetails, destination); break;
      case 139: m_TagHandler.start_para_lit(tagName, style, level, eltDetails, destination); break;
      case 140: m_TagHandler.start_para_m(tagName, style, level, eltDetails, destination); break;
      case 141: m_TagHandler.start_para_mi(tagName, style, level, eltDetails, destination); break;
      case 142: m_TagHandler.start_para_mr(tagName, style, level, eltDetails, destination); break;
      case 143: m_TagHandler.start_para_ms(tagName, style, level, eltDetails, destination); break;
      case 144: m_TagHandler.start_para_ms1(tagName, style, level, eltDetails, destination); break;
      case 145: m_TagHandler.start_para_ms2(tagName, style, level, eltDetails, destination); break;
      case 146: m_TagHandler.start_para_ms3(tagName, style, level, eltDetails, destination); break;
      case 147: m_TagHandler.start_para_mt(tagName, style, level, eltDetails, destination); break;
      case 148: m_TagHandler.start_para_mt1(tagName, style, level, eltDetails, destination); break;
      case 149: m_TagHandler.start_para_mt2(tagName, style, level, eltDetails, destination); break;
      case 150: m_TagHandler.start_para_mt3(tagName, style, level, eltDetails, destination); break;
      case 151: m_TagHandler.start_para_mt4(tagName, style, level, eltDetails, destination); break;
      case 152: m_TagHandler.start_para_mte(tagName, style, level, eltDetails, destination); break;
      case 153: m_TagHandler.start_para_mte1(tagName, style, level, eltDetails, destination); break;
      case 154: m_TagHandler.start_para_mte2(tagName, style, level, eltDetails, destination); break;
      case 155: m_TagHandler.start_para_nb(tagName, style, level, eltDetails, destination); break;
      case 156: m_TagHandler.start_para_no_break_verse_paragraph(tagName, style, level, eltDetails, destination); break;
      case 157: m_TagHandler.start_para_p(tagName, style, level, eltDetails, destination); break;
      case 158: m_TagHandler.start_para_p_introduction(tagName, style, level, eltDetails, destination); break;
      case 159: m_TagHandler.start_para_p_letter_closing(tagName, style, level, eltDetails, destination); break;
      case 160: m_TagHandler.start_para_p_letter_opening(tagName, style, level, eltDetails, destination); break;
      case 161: m_TagHandler.start_para_p_variant(tagName, style, level, eltDetails, destination); break;
      case 162: m_TagHandler.start_para_p1(tagName, style, level, eltDetails, destination); break;
      case 163: m_TagHandler.start_para_p2(tagName, style, level, eltDetails, destination); break;
      case 164: m_TagHandler.start_para_pb(tagName, style, level, eltDetails, destination); break;
      case 165: m_TagHandler.start_para_pc(tagName, style, level, eltDetails, destination); break;
      case 166: m_TagHandler.start_para_periph(tagName, style, level, eltDetails, destination); break;
      case 167: m_TagHandler.start_para_pi(tagName, style, level, eltDetails, destination); break;
      case 168: m_TagHandler.start_para_pi1(tagName, style, level, eltDetails, destination); break;
      case 169: m_TagHandler.start_para_pi2(tagName, style, level, eltDetails, destination); break;
      case 170: m_TagHandler.start_para_pi3(tagName, style, level, eltDetails, destination); break;
      case 171: m_TagHandler.start_para_pm(tagName, style, level, eltDetails, destination); break;
      case 172: m_TagHandler.start_para_pmc(tagName, style, level, eltDetails, destination); break;
      case 173: m_TagHandler.start_para_pmo(tagName, style, level, eltDetails, destination); break;
      case 174: m_TagHandler.start_para_pmr(tagName, style, level, eltDetails, destination); break;
      case 175: m_TagHandler.start_para_psa(tagName, style, level, eltDetails, destination); break;
      case 176: m_TagHandler.start_para_psa_conclusion(tagName, style, level, eltDetails, destination); break;
      case 177: m_TagHandler.start_para_psa_continuation(tagName, style, level, eltDetails, destination); break;
      case 178: m_TagHandler.start_para_psa_introduction(tagName, style, level, eltDetails, destination); break;
      case 179: m_TagHandler.start_para_q(tagName, style, level, eltDetails, destination); break;
      case 180: m_TagHandler.start_para_q1(tagName, style, level, eltDetails, destination); break;
      case 181: m_TagHandler.start_para_q2(tagName, style, level, eltDetails, destination); break;
      case 182: m_TagHandler.start_para_q3(tagName, style, level, eltDetails, destination); break;
      case 183: m_TagHandler.start_para_q4(tagName, style, level, eltDetails, destination); break;
      case 184: m_TagHandler.start_para_qa(tagName, style, level, eltDetails, destination); break;
      case 185: m_TagHandler.start_para_qc(tagName, style, level, eltDetails, destination); break;
      case 186: m_TagHandler.start_para_qm(tagName, style, level, eltDetails, destination); break;
      case 187: m_TagHandler.start_para_qm1(tagName, style, level, eltDetails, destination); break;
      case 188: m_TagHandler.start_para_qm2(tagName, style, level, eltDetails, destination); break;
      case 189: m_TagHandler.start_para_qm3(tagName, style, level, eltDetails, destination); break;
      case 190: m_TagHandler.start_para_qr(tagName, style, level, eltDetails, destination); break;
      case 191: m_TagHandler.start_para_qr_attribution(tagName, style, level, eltDetails, destination); break;
      case 192: m_TagHandler.start_para_qr_refrain(tagName, style, level, eltDetails, destination); break;
      case 193: m_TagHandler.start_para_qr_selah(tagName, style, level, eltDetails, destination); break;
      case 194: m_TagHandler.start_para_r(tagName, style, level, eltDetails, destination); break;
      case 195: m_TagHandler.start_para_rem(tagName, style, level, eltDetails, destination); break;
      case 196: m_TagHandler.start_para_restore(tagName, style, level, eltDetails, destination); break;
      case 197: m_TagHandler.start_para_s(tagName, style, level, eltDetails, destination); break;
      case 198: m_TagHandler.start_para_s_variant(tagName, style, level, eltDetails, destination); break;
      case 199: m_TagHandler.start_para_s_variant_tail(tagName, style, level, eltDetails, destination); break;
      case 200: m_TagHandler.start_para_s1(tagName, style, level, eltDetails, destination); break;
      case 201: m_TagHandler.start_para_s2(tagName, style, level, eltDetails, destination); break;
      case 202: m_TagHandler.start_para_s3(tagName, style, level, eltDetails, destination); break;
      case 203: m_TagHandler.start_para_s4(tagName, style, level, eltDetails, destination); break;
      case 204: m_TagHandler.start_para_sp(tagName, style, level, eltDetails, destination); break;
      case 205: m_TagHandler.start_para_sr(tagName, style, level, eltDetails, destination); break;
      case 206: m_TagHandler.start_para_toc1(tagName, style, level, eltDetails, destination); break;
      case 207: m_TagHandler.start_para_toc2(tagName, style, level, eltDetails, destination); break;
      case 208: m_TagHandler.start_para_toc3(tagName, style, level, eltDetails, destination); break;
      case 209: m_TagHandler.start_para_tr(tagName, style, level, eltDetails, destination); break;
      case 364: m_TagHandler.start_reference(tagName, style, level, eltDetails, destination); break;
      case 365: m_TagHandler.start_usx(tagName, style, level, eltDetails, destination); break;
      case 366: m_TagHandler.start_usxv(tagName, style, level, eltDetails, destination); break;
      case 367: m_TagHandler.start_v(tagName, style, level, eltDetails, destination); break;
      case 368: m_TagHandler.start_verse_v(tagName, style, level, eltDetails, destination); break;
    }

    m_TagHandler.afterUsxStartTag(tagName, style, level, eltDetails, destination);
  }


  /****************************************************************************/
  private String m_ActiveTagsWithLevels = "";
  private String m_ActiveTagsWithoutLevels = "";
  private boolean m_AutoGenerateToc1 = true;
  private HashMap<String, Integer> m_DestinationTable = new HashMap<String, Integer>();
  private String m_MostRecentParaTagDetails = "";
  private static final Pattern m_NumberedTagPattern = Pattern.compile("([A-Za-z]+)(\\d*)");
  private SAXParser m_Parser = new SAXParser();
  private int m_PrevDestination = OutputManager.C_Destination_BitBucket;
  private HashMap<String, Integer> m_SwitchTable = new HashMap<String, Integer>();
  private UsxConverterTo m_TagHandler = null;
  private Stack<String> m_TagStack = new Stack<String>();
  private String m_UbsAbbreviation = null;
  private Stack<String> m_UserDefinedRem = new Stack<String>();





  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                            Xerces interface                            **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Element details as provided by SAX.
  */

  protected class ElementDetails
  {
    HashMap<String, String> m_Attrs = new HashMap<String, String>();
    String m_Name = "";
    String m_NodeValue = "";
    String m_qName = "";
    String m_Uri = "";

    public ElementDetails (String uri,
			   String local,
			   String qName,
			   String nodeValue,
			   Attributes attrs)
    { 
      if (null != attrs) for (int i = 0; i < attrs.getLength(); ++i) m_Attrs.put(attrs.getLocalName(i), attrs.getValue(i));
      m_Name = local.trim();
      m_NodeValue = nodeValue;
      m_qName = qName.trim();
      m_Uri = uri;
    }

    public String getAttribute (String attrName) { return m_Attrs.get(attrName); }
    public String getAttribute (String attrName, String dflt) { try { return m_Attrs.get(attrName); } catch (Exception e) { return dflt; } }
    public String getName () { return m_Name; }
    public String getNodeValue () { return m_NodeValue; }
    public String getqName () { return m_qName; }
    public String getUri () { return m_Uri; }
    public void setAttribute (String attrName, String val) { m_Attrs.put(attrName, val); }
  }


  /****************************************************************************/
  private StringBuilder m_BufferedText = new StringBuilder();


  /****************************************************************************/
  /* Internal stuff for use with SAX callbacks.  Note that the
     routines are public only because the SAX ContentHandler interface
     demands it -- nothing else is expected to call them directly.
     The implementation below invokes my own analogues of these
     routines, which provide the benefit of some measure of uniformity
     between DOM and SAX processing -- see the head of file comments.
     If you want to take advantage of this, then you'll need to
     provide implementations of my versions -- startDocument(Object
     obj), etc.  If not, then you're free to override the
     implementations below and work with SAX directly. */




  @Override public void startElement (String uri, String local, String qName, Attributes attrs)
  {
    /**************************************************************************/
    /* Special processing for toc1.  We have concluded that we always want a
     * toc1 entry, just after the book tag, in any incoming USX.  However,
     * experience suggests that very few translators actually provide one.
     * I have therefore decided to simulate one within the present class.
     * For this to work properly, I need to remove any toc1 which _has_
     * actually been supplied, which is the purpose of the processing below.
     * If I encounter a toc1, I push a null on to the tag stack, to flag the
     * fact that we need to ignore stuff for the time being.  If the present
     * routine is called at a time when there is a null on the top of the
     * stack, then I just push a new null.  The methods "endElement" and
     * "characters" both use this information to determine what to do, and
     * endElement also pulls stuff off the stack again.
     */
    
    if (m_AutoGenerateToc1)
    {
      String s = attrs.getValue("style");
      if (null != s && "toc1".equals(s)) { m_TagStack.push(null); return; }
      if (!m_TagStack.isEmpty() && null == m_TagStack.peek()) { m_TagStack.push(null); return; }
    }
    
    
    
    /**************************************************************************/
    if ("book".equals(local)) m_UbsAbbreviation = attrs.getValue("code");
    
    
    
    /**************************************************************************/
    jamieCheckForBufferedText();
    ElementDetails eltDetails = new ElementDetails(uri, local, qName, "", attrs);
    try
    {
      routeStart(eltDetails);
    }
    catch (ConverterException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new ConverterException(ConverterException.Action.AbandonFileWhenever, ConverterException.Log.Error, e.getMessage());
    }
  }

  @Override public void endElement (String uri, String local, String qName)
  {
    /**************************************************************************/
    /* See comments about toc1 in startElement. */
    
    if (m_AutoGenerateToc1 && null == m_TagStack.peek()) { m_TagStack.pop(); return; }
    

    
    /**************************************************************************/
    jamieCheckForBufferedText();
    ElementDetails eltDetails = new ElementDetails(uri, local, qName, "", null);

    try
    { 
      routeEnd(eltDetails); 
    }
    catch (ConverterException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new ConverterException(ConverterException.Action.AbandonFileWhenever, ConverterException.Log.Error, e.getMessage());
    }

    
    
    /**************************************************************************/
    /* If this is a book tag, and if we're autogenerating toc1 entries, then
     * we need to do it now.
     */
    
    if (m_AutoGenerateToc1 && "book".equals(local))
    {
      String s = BibleDetails.instance().getFullVernacularName_E(m_UbsAbbreviation);
      ElementDetails textDetails = new ElementDetails("", "#text", "#text", s, null);
      ElementDetails toc1Details = new ElementDetails(uri, "para", qName, "", null);
      toc1Details.setAttribute("style", "toc1");
      
      try
      {
        routeStart(toc1Details);
        routeStart(textDetails);
        routeEnd(textDetails);
        routeEnd(toc1Details);
      }
      catch (ConverterException e)
      {
        throw e;
      }
      catch (Exception e)
      {
        throw new ConverterException(ConverterException.Action.AbandonFileWhenever, ConverterException.Log.Error, e.getMessage());
      }
    }
  }

  @Override public void characters(char[] text, int start, int length)
  {
    if (!m_AutoGenerateToc1 || (null != m_TagStack.peek())) m_BufferedText.append(text, start, length);
  }

  private void jamieCheckForBufferedText () throws ConverterException
  {
    String s = m_BufferedText.toString();
    s = s.replaceFirst("^\\s+", "")
         .replaceFirst("^[\\n\\r]+", "")
         .replaceFirst("[\\n\\r]+$", "");
    
    if (0 == s.length()) return;
    ElementDetails eltDetails = new ElementDetails("", "#text", "#text", s, null);
    try 
    { 
      routeStart(eltDetails);
      routeEnd(eltDetails); 
    }
    catch (ConverterException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new ConverterException(ConverterException.Action.AbandonFileWhenever, ConverterException.Log.Error, e.getMessage());
    }
    finally 
    { 
      m_BufferedText = new StringBuilder();
    }
  }

  @Override public void startDocument () {}
  @Override public void endDocument () {}
  @Override public void setDocumentLocator (Locator locator) {}
  @Override public void startPrefixMapping (String prefix, String uri) {}
  @Override public void endPrefixMapping (String prefix) {}
  @Override public void ignorableWhitespace (char[] text, int start, int length) throws SAXException {}
  @Override public void processingInstruction (String target, String data) {}
  @Override public void skippedEntity (String name) {}
}
